
package fr.umlv.m2.jee.portailEtudiant.service.Impl;

import fr.umlv.m2.jee.portailEtudiant.persistence.*;
import fr.umlv.m2.jee.portailEtudiant.service.DisplayCourse;
import fr.umlv.m2.jee.portailEtudiant.service.DisplayFormation;
import fr.umlv.m2.jee.portailEtudiant.service.IDisplayFormationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: soufiane
 * Date: 18/04/11
 * Time: 21:31
 * To change this template use File | Settings | File Templates.
 */
@Service("defaultDisplayFormationService")
public class DefaultDisplayFormationService implements IDisplayFormationService{



    @Autowired
    @Qualifier("hibernateFormationDAO")
    private IFormationDAO dao;

    @Autowired
    @Qualifier("hibernateCourseDAO")
    private ICourseDAO daoCourse;

    @Autowired
    @Qualifier("hibernateTdDAO")
    private ITdDAO tdDao;

    @Autowired
    @Qualifier("hibernateTpDAO")
    private ITpDAO tpDao;

    @Autowired
    @Qualifier("hibernateTutorialDAO")
    private ITutorialDAO tutorialDao;

    @Autowired
    @Qualifier("hibernateCourseDocumentDAO")
    private ICourseDocumentDAO courseDocumentDAO;


    @Override
    @Transactional(readOnly=true)
    public List<DisplayFormation> getAllDisplayFormation() {
        List< DisplayFormation > result = new ArrayList< DisplayFormation >();

        for(Formation formation : dao.findAll()) {
            result.add(new DisplayFormation(formation));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public List<DisplayCourse> getAllCourseById(String id) {
        List< DisplayCourse > result = new ArrayList< DisplayCourse >();

        for(Course course : dao.findCourseById(id)) {
            result.add(new DisplayCourse(course));
        }
        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplayFormation getFormationByName(String id) {
        return new DisplayFormation(dao.findFormationByName(id));
    }

    @Override
    public DisplayFormation getFormationById(long id) {
        return new DisplayFormation(dao.findById(id));
    }

    @Override
    @Transactional(readOnly=true)
    public void removeFormation(String id) {
        Formation f = dao.findFormationByName(id);
        List<Course> cours = f.getCourse();
        for(Course c : cours){
            for(Td td : daoCourse.findTdsByCourseId(c.getName()))
            {
                   tdDao.remove(td);
            }
            for(Tp tp : daoCourse.findTpsByCourseId(c.getName()))
            {
                   tpDao.remove(tp);
            }
            for(Tutorial tutorial : daoCourse.findTutorialsByCourseId(c.getName()))
            {
                   tutorialDao.remove(tutorial);
            }
            for(CourseDocument courseDocument : daoCourse.findDocumentByCourseId(c.getName()))
            {
                   courseDocumentDAO.remove(courseDocument);
            }
            this.daoCourse.refresh(c);
            this.daoCourse.remove(c);

        }
        dao.refresh(f);
        dao.remove(f);


    }

    @Override
    @Transactional(readOnly=true)
    public void update(DisplayFormation formation) {
        dao.merge(formation.getFormation());
    }

    @Override
    @Transactional(readOnly=true)
    public void addFormation(String nom) {
        Formation formation = new Formation();
        formation.setName(nom);
        dao.persist(formation);
    }

    @Override
    @Transactional(readOnly=true)
    public void addCourse( String nameFormation, String nameCourse) {
        Formation formation = dao.findFormationByName(nameFormation);
        Course course = new Course();
        course.setName(nameCourse);

        course.setFormation(formation);

        daoCourse.persist(course);

        dao.merge(formation);
    }

    @Override
    public void editCourse(String nameFormation, String nameCourse) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

